# -*- coding: utf-8 -*-

# graphic-pppoe-client - Easy graphic networking manager
#
# Author: GoTLiuM InSPiRiT <gotlium@gmail.com>
#
# Copyright 2010, GoTLiuM InSPiRiT <gotlium@gmail.com>.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

from __init__ import *
from libraries.fs import SQL_FILES_DIR, fileExists, getFile
from libraries.los import msg

''' Requests to database from main loop '''
class MainModel(object):
	''' Loading modules list from "modules" table '''
	@dbs
	def getModules(self, where = {'is_enabled': 'true'}):
		modules = db.select('modules', where)
		return modules

	def getAllModules(self):
		return self.getModules({})

	''' Loading sql requests from file '''
	@dbs
	def loadTables(self, tableName):
		sqlFile = '%s/%s.sql' % (SQL_FILES_DIR, tableName)
		if fileExists(sqlFile):
			for sql in getFile(sqlFile).split(';;'):
				db.query(sql)

	''' Changing module status activity '''
	@dbs
	def setEnabled(self, flag, module):
		db.update('modules',
			{'is_enabled': flag}, {'alias': module}
		)
		if flag == 'true':
			msg("Module '%s' is enabled." % module)
		else:
			msg("Module '%s' is disabled." % module)

	''' On exit, we are closing db file '''
	def _onExit(self):
		msg("Database connection closed.")
		db.disconnect()
